﻿using System;

namespace DotNetImage.Imaging.Filters
{
    public class ContrastFilter : FilterBase
    {
        [ParameterAttribute(Name = "Contrast", DefaultValue = 5, MinValue = -50, MaxValue = 50, Step = 0.1)]
        public int Contrast { get; set; }

        private double multiplier = 0;

        protected override void ValidateArguments()
        {
            Validator.ValidateInclusiveRange("Contrast", Contrast, -50, 50);

            // Prepare value for multiplication
            multiplier = Math.Pow((100.0 + Contrast) / 100.0, 2);
        }

        private uint ProcessColor(double input)
        {
            double maxValue = (double)Subject.ColorChannelMaxValue;
            double result = ((input / maxValue - 0.5) * multiplier + 0.5) * maxValue;
            return (uint)result;
        }

        protected override void ProcessPixel(ref Pixel pix, uint x, uint y)
        {
            pix.Red = ProcessColor(pix.Red);
            pix.Green = ProcessColor(pix.Green);
            pix.Blue = ProcessColor(pix.Blue);
        }
    }
}
